<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Language" content="zh-cn">
    
    
    <title>
        
            LeetCode笔记-KMP | Nefelibata
             | Nefelibata
        
    </title>
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    
        <meta name="theme-color" content="#77AAFF">
    
    
    <meta name="keywords" content="LeetCode">
    
    

    

    <!-- Baidu Push -->
<script>
	(function(){
		var bp = document.createElement('script');
		var curProtocol = window.location.protocol.split(':')[0];
		if (curProtocol === 'https') {
			bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
		}
		else {
			bp.src = 'http://push.zhanzhang.baidu.com/push.js';
		}
		var s = document.getElementsByTagName("script")[0];
		s.parentNode.insertBefore(bp, s);
	})();

	var _hmt = _hmt || [];
</script>



    
    <meta name="description" content="KMP算法">
<meta property="og:type" content="article">
<meta property="og:title" content="LeetCode笔记-KMP">
<meta property="og:url" content="https://nefelibata.icu/2022/06e8411ac8.html">
<meta property="og:site_name" content="Nefelibata">
<meta property="og:description" content="KMP算法">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://code-thinking.cdn.bcebos.com/gifs/KMP%E7%B2%BE%E8%AE%B23.gif">
<meta property="article:published_time" content="2022-06-13T13:30:27.000Z">
<meta property="article:modified_time" content="2024-01-04T02:41:48.345Z">
<meta property="article:author" content="Nefelibata">
<meta property="article:tag" content="LeetCode">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://code-thinking.cdn.bcebos.com/gifs/KMP%E7%B2%BE%E8%AE%B23.gif">
    
        <link rel="alternate" type="application/atom+xml" title="Nefelibata" href="/blog/atom.xml">
    
    <link rel="shortcut icon" href="/blog/img/favicon.ico">
    <link id="style" rel="stylesheet" href="/blog/css/style.css?v=3.0">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    
<meta name="generator" content="Hexo 6.3.0">
<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
</head>

<body>
    <div id="loading" class="active"></div>
    <aside id="menu"  class >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" >
      <div class="brand">
        <a class="avatar waves-effect waves-circle waves-light">
          <img src="/blog/img/avatar.jpg" alt="avatar">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname" id="name">Nefelibata</h5>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
              <li class="waves-block waves-effect">
                  <a href="/blog/" target="_self" >
                    <i class="icon icon-lg icon-home"></i>
                    <span>主 页</span><i class="icon icon-lg icon-caret-left"></i>
                  </a>
              </li>
            
              <li class="waves-block waves-effect">
                  <a href="/blog/archives" target="_self" >
                    <i class="icon icon-lg icon-archives"></i>
                    <span>归 档</span><i class="icon icon-lg icon-caret-left"></i>
                  </a>
              </li>
            
              <li class="waves-block waves-effect">
                  <a href="/blog/categories" target="_self" >
                    <i class="icon icon-lg icon-th-list"></i>
                    <span>分 类</span><i class="icon icon-lg icon-caret-left"></i>
                  </a>
              </li>
            
              <li class="waves-block waves-effect">
                  <a href="/blog/tags" target="_self" >
                    <i class="icon icon-lg icon-tags"></i>
                    <span>标 签</span><i class="icon icon-lg icon-caret-left"></i>
                  </a>
              </li>
            
              <li class="waves-block waves-effect">
                  <a href="/blog/../gallery" target="_blank" >
                    <i class="icon icon-lg icon-image"></i>
                    <span>相册</span><i class="icon icon-lg icon-caret-left"></i>
                  </a>
              </li>
            
              <li class="waves-block waves-effect">
                  <a href="/blog/../about" target="_blank" >
                    <i class="icon icon-lg icon-smile-o"></i>
                    <span>关 于</span><i class="icon icon-lg icon-caret-left"></i>
                  </a>
              </li>
            
              <li class="waves-block waves-effect">
                  <a href="/blog/../" target="_self" >
                    <i class="icon icon-lg icon-tree"></i>
                    <span>Nefelibata</span><i class="icon icon-lg icon-caret-left"></i>
                  </a>
              </li>
            
      <div class="nav2">
          
              <a class="nav2item" data-title="Email" href="mailto:kunpengl0111@gamil.com" target="_parent"title="Email" >
                <i class="icon icon-lg icon-envelope-o envelope-o"></i>
              </a>
          
              <a class="nav2item" data-title="Github" href="https://github.com/kpl0111" target="_blank"title="Github" >
                <i class="icon icon-lg icon-github github"></i>
              </a>
          
              <a class="nav2item" data-title="Twitter" href="https://twitter.com/Nefelib31847846" target="_blank"title="Twitter" >
                <i class="icon icon-lg icon-twitter twitter"></i>
              </a>
          
        </div>
      </ul>
    </div>
  </div>
</aside>


    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">LeetCode笔记-KMP</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        <a href="../../atom.xml" target="_blank" class="header-icon waves-effect waves-circle waves-light" id="Rss">
            <i class="icon icon-lg icon-rss"></i>
        </a>
    </div>
</header>
<header class="content-header post-header">
    <div class="container fade-scale">
        <div id="myheader">
            <h1 class="title">
                
            </h1>
            <h5 class="subtitle">
                
                
            </h5>
        </div>
    </div>
</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            
                <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#%E5%AE%9E%E7%8E%B0strStr"><span class="post-toc-text">实现strStr()</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#%E6%91%98%E8%A6%81"><span class="post-toc-text">摘要</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#%E6%9E%84%E9%80%A0next%E6%95%B0%E7%BB%84"><span class="post-toc-text">构造next数组</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#%E4%BD%BF%E7%94%A8next%E6%95%B0%E7%BB%84%E6%9D%A5%E5%81%9A%E5%8C%B9%E9%85%8D"><span class="post-toc-text">使用next数组来做匹配</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#%E5%89%8D%E7%BC%80%E8%A1%A8%E7%BB%9F%E4%B8%80%E5%87%8F%E4%B8%80-C-%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="post-toc-text">前缀表统一减一 C++代码实现</span></a></li></ol></li></ol>
            
        </nav>
    </aside>
   
<article id="post-LeetCode笔记-KMP"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">LeetCode笔记-KMP</h1>
        <div class="post-meta">
            <i class="icon icon-lg icon-calendar-o"></i>
            发表于
            <time class="post-time" title="2022-06-13 21:30:27" datetime="2022-06-13T13:30:27.000Z"  itemprop="datePublished">2022-06-13</time>

            <br id="mybreak"/>
            
	<i class="icon icon-lg icon-folder-o"></i>
	分类：<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/%E7%AC%94%E8%AE%B0/">笔记</a></li></ul>


            <i>·</i>
        </div>
        <div class="post-count-custom">
            <i class="icon icon-lg icon-comment-o"></i>
            阅读本文可能花费您&nbsp;<span class="post-count">9.9</span>&nbsp;分钟
        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <p>KMP算法</p>
<span id="more"></span>

<blockquote>
<p>本篇为个人笔记，内容或有错误。<br>图片部分源于<a target="_blank" rel="noopener" href="https://programmercarl.com/">代码随想录</a>，侵删。</p>
</blockquote>
<p>字符串操作主要考虑格式的转换。</p>
<h2 id="实现strStr"><a href="#实现strStr" class="headerlink" title="实现strStr()"></a>实现strStr()</h2><p><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/implement-strstr/">LeetCode题目链接</a></p>
<p>KMP的经典思想就是:当出现字符串不匹配时，可以记录一部分之前已经匹配的文本内容，利用这些信息避免从头再去做匹配。</p>
<p><a target="_blank" rel="noopener" href="https://programmercarl.com/0028.%E5%AE%9E%E7%8E%B0strStr.html">KMP精讲文字版</a></p>
<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1PD4y1o7nd/?spm_id_from=pageDriver&amp;vd_source=0febcf63c24b3fda1cd2c47a2078ae54">KMP精讲视频</a></p>
<h2 id="摘要"><a href="#摘要" class="headerlink" title="摘要"></a>摘要</h2><h3 id="构造next数组"><a href="#构造next数组" class="headerlink" title="构造next数组"></a>构造next数组</h3><blockquote>
<p>next数组的本质就是前缀后缀相同的字符位数表<br>实现方式常见的有减一、右移和保持不变三种，但是本质都是一样的，只在细微地方有区别，本文采用减一方式，其他方式代码详见<a target="_blank" rel="noopener" href="https://programmercarl.com/0028.%E5%AE%9E%E7%8E%B0strStr.html#%E5%89%8D%E7%BC%80%E8%A1%A8-%E4%B8%8D%E5%87%8F%E4%B8%80-c-%E5%AE%9E%E7%8E%B0">前缀表（不减一）C++实现</a></p>
</blockquote>
<p>我们定义一个函数getNext来构建next数组，函数参数为指向next数组的指针，和一个字符串。 代码如下：</p>
<pre><code class="C++">void getNext(int* next, const string&amp; s)
</code></pre>
<p>构造next数组其实就是计算模式串s，前缀表的过程。 主要有如下三步：</p>
<ol>
<li>初始化</li>
<li>处理前后缀不相同的情况</li>
<li>处理前后缀相同的情况</li>
</ol>
<p>接下来我们详解详解一下。</p>
<p><strong>初始化：</strong><br>定义两个指针i和j，j指向前缀末尾位置，i指向后缀末尾位置。</p>
<p>然后还要对next数组进行初始化赋值，如下：</p>
<pre><code class="C++">int j = -1;
next[0] = j;
</code></pre>
<p>j 为什么要初始化为 -1呢，因为之前说过 前缀表要统一减一的操作仅仅是其中的一种实现，我们这里选择j初始化为-1，下文我还会给出j不初始化为-1的实现代码。</p>
<p>next[i] 表示 i（包括i）之前最长相等的前后缀长度（其实就是j）</p>
<p>所以初始化next[0] = j 。</p>
<p>处理前后缀不相同的情况<br>因为j初始化为-1，那么i就从1开始，进行s[i] 与 s[j+1]的比较。</p>
<p>所以遍历模式串s的循环下标i 要从 1开始，代码如下：</p>
<pre><code class="C++">for (int i = 1; i &lt; s.size(); i++) {
</code></pre>
<p>如果 s[i] 与 s[j+1]不相同，也就是遇到 前后缀末尾不相同的情况，就要向前回退。</p>
<p>怎么回退呢？</p>
<p>next[j]就是记录着j（包括j）之前的子串的相同前后缀的长度。</p>
<p>那么 s[i] 与 s[j+1] 不相同，就要找 j+1前一个元素在next数组里的值（就是next[j]）。</p>
<p>所以，处理前后缀不相同的情况代码如下：</p>
<pre><code class="C++">while (j &gt;= 0 &amp;&amp; s[i] != s[j + 1]) { // 前后缀不相同了
    j = next[j]; // 向前回退
}
</code></pre>
<p>处理前后缀相同的情况<br>如果 s[i] 与 s[j + 1] 相同，那么就同时向后移动i 和j 说明找到了相同的前后缀，同时还要将j（前缀的长度）赋给next[i], 因为next[i]要记录相同前后缀的长度。</p>
<p>代码如下：</p>
<pre><code class="C++">if (s[i] == s[j + 1]) { // 找到相同的前后缀
    j++;
}
next[i] = j;
</code></pre>
<p>最后整体构建next数组的函数代码如下：</p>
<pre><code class="C++">void getNext(int* next, const string&amp; s){
    int j = -1;
    next[0] = j;
    for(int i = 1; i &lt; s.size(); i++) { // 注意i从1开始
        while (j &gt;= 0 &amp;&amp; s[i] != s[j + 1]) { // 前后缀不相同了
            j = next[j]; // 向前回退
        }
        if (s[i] == s[j + 1]) { // 找到相同的前后缀
            j++;
        }
        next[i] = j; // 将j（前缀的长度）赋给next[i]
    }
}
</code></pre>
<p>代码构造next数组的逻辑流程动画如下：</p>
<p>得到了next数组之后，就要用这个来做匹配了。</p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="https://code-thinking.cdn.bcebos.com/gifs/KMP%E7%B2%BE%E8%AE%B23.gif" alt="" title="">
                </div>
                <div class="image-caption"></div>
            </figure>

<h3 id="使用next数组来做匹配"><a href="#使用next数组来做匹配" class="headerlink" title="使用next数组来做匹配"></a>使用next数组来做匹配</h3><p>在文本串s里 找是否出现过模式串t。</p>
<p>定义两个下标j 指向模式串起始位置，i指向文本串起始位置。</p>
<p>那么j初始值依然为-1，为什么呢？ 依然因为next数组里记录的起始位置为-1。</p>
<p>i就从0开始，遍历文本串，代码如下：</p>
<pre><code class="C++">for (int i = 0; i &lt; s.size(); i++) 
</code></pre>
<p>接下来就是 s[i] 与 t[j + 1] （因为j从-1开始的） 进行比较。</p>
<p>如果 s[i] 与 t[j + 1] 不相同，j就要从next数组里寻找下一个匹配的位置。</p>
<p>代码如下：</p>
<pre><code class="C++">while(j &gt;= 0 &amp;&amp; s[i] != t[j + 1]) {
    j = next[j];
}
</code></pre>
<p>如果 s[i] 与 t[j + 1] 相同，那么i 和 j 同时向后移动， 代码如下：</p>
<pre><code class="C++">if (s[i] == t[j + 1]) {
    j++; // i的增加在for循环里
}
</code></pre>
<p>如何判断在文本串s里出现了模式串t呢，如果j指向了模式串t的末尾，那么就说明模式串t完全匹配文本串s里的某个子串了。</p>
<p>本题要在文本串字符串中找出模式串出现的第一个位置 (从0开始)，所以返回当前在文本串匹配模式串的位置i 减去 模式串的长度，就是文本串字符串中出现模式串的第一个位置。</p>
<p>代码如下：</p>
<pre><code class="C++">if (j == (t.size() - 1) ) {
    return (i - t.size() + 1);
}
</code></pre>
<p>那么使用next数组，用模式串匹配文本串的整体代码如下：</p>
<pre><code class="C++">int j = -1; // 因为next数组里记录的起始位置为-1
for (int i = 0; i &lt; s.size(); i++) { // 注意i就从0开始
    while(j &gt;= 0 &amp;&amp; s[i] != t[j + 1]) { // 不匹配
        j = next[j]; // j 寻找之前匹配的位置
    }
    if (s[i] == t[j + 1]) { // 匹配，j和i同时向后移动
        j++; // i的增加在for循环里
    }
    if (j == (t.size() - 1) ) { // 文本串s里出现了模式串t
        return (i - t.size() + 1);
    }
}
</code></pre>
<p>此时所有逻辑的代码都已经写出来了，力扣 28.实现strStr 题目的整体代码如下：</p>
<h3 id="前缀表统一减一-C-代码实现"><a href="#前缀表统一减一-C-代码实现" class="headerlink" title="前缀表统一减一 C++代码实现"></a>前缀表统一减一 C++代码实现</h3><pre><code class="C++">class Solution {
public:
    void getNext(int* next, const string&amp; s) {
        int j = -1;
        next[0] = j;
        for(int i = 1; i &lt; s.size(); i++) { // 注意i从1开始
            while (j &gt;= 0 &amp;&amp; s[i] != s[j + 1]) { // 前后缀不相同了
                j = next[j]; // 向前回退
            }
            if (s[i] == s[j + 1]) { // 找到相同的前后缀
                j++;
            }
            next[i] = j; // 将j（前缀的长度）赋给next[i]
        }
    }
    int strStr(string haystack, string needle) {
        if (needle.size() == 0) {
            return 0;
        }
        int next[needle.size()];
        getNext(next, needle);
        int j = -1; // // 因为next数组里记录的起始位置为-1
        for (int i = 0; i &lt; haystack.size(); i++) { // 注意i就从0开始
            while(j &gt;= 0 &amp;&amp; haystack[i] != needle[j + 1]) { // 不匹配
                j = next[j]; // j 寻找之前匹配的位置
            }
            if (haystack[i] == needle[j + 1]) { // 匹配，j和i同时向后移动
                j++; // i的增加在for循环里
            }
            if (j == (needle.size() - 1) ) { // 文本串s里出现了模式串t
                return (i - needle.size() + 1);
            }
        }
        return -1;
    }
};
</code></pre>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新：<time datetime="2024-01-04T02:41:48.345Z" itemprop="dateUpdated">2024-01-04 10:41:48</time>
</span>


        
        
        
    </div>
    <footer>
        <div onclick="location.href='https://nefelibata.icu'">
            <img src="/blog/img/avatar.jpg" alt="Nefelibata">
            <a>Nefelibata</a>
        </div>
    </footer>
</blockquote>

        
    <div class="page-reward">
        <nav class="myreward">
            <a id="rewardBtn" href="javascript:;"><span>打&nbsp;赏</span><span>装成好像很多人打赏的样子</span></a>
        </nav>
    </div>



        <div class="post-footer">
            
	<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/LeetCode/" rel="tag">LeetCode</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://nefelibata.icu/2022/06e8411ac8.html&title=《LeetCode笔记-KMP》 — Nefelibata&pic=https://nefelibata.icu/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://nefelibata.icu/2022/06e8411ac8.html&title=《LeetCode笔记-KMP》 — Nefelibata&source=KMP算法" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://nefelibata.icu/2022/06e8411ac8.html" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《LeetCode笔记-KMP》 — Nefelibata&url=https://nefelibata.icu/2022/06e8411ac8.html&via=https://nefelibata.icu" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://nefelibata.icu/2022/06e8411ac8.html" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/blog/2022/063572401b.html" id="post-prev" class="post-nav-link">
        <h4 class="title" >
          上一篇：LeetCode笔记-字符串
        </h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/blog/2022/064cfc9b66.html" id="post-next" class="post-nav-link">
        <h4 class="title" data-hover="下一篇：LeetCode笔记-哈希表">下一篇：LeetCode笔记-哈希表</h4>
      </a>
    </div>
  
</nav>


</article>

</div>

        <footer class="footer">
    <div class="footer-content">
        <span class="power">
            <i class="icon icon-lg icon-copyright"></i>
            2021
            <i class="icon icon-lg icon-heart"></i>
            <a href="https://nefelibata.icu">nefelibata.icu</a>
        </span>
    </div>
</footer>

    </main>
    
        
<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        <span>感谢您的鼓励支持！</span>
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" data-img="/blog/img/dog.png" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="/blog/img/wechat.png" data-alipay="/blog/img/alipay.png">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechatPay">&nbsp;&nbsp;微信&nbsp;&nbsp;</span>
                <span class="reward-toggle-item alipayPay">支付宝</span>
            </div>
        </label>
        
        <i class="icon icon-caret-up"></i>
    </div>
</div>


    
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://nefelibata.icu/2022/06e8411ac8.html&title=《LeetCode笔记-KMP》 — Nefelibata&pic=https://nefelibata.icu/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://nefelibata.icu/2022/06e8411ac8.html&title=《LeetCode笔记-KMP》 — Nefelibata&source=KMP算法" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://nefelibata.icu/2022/06e8411ac8.html" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《LeetCode笔记-KMP》 — Nefelibata&url=https://nefelibata.icu/2022/06e8411ac8.html&via=https://nefelibata.icu" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://nefelibata.icu/2022/06e8411ac8.html" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p class="wechatshare">扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <!-- waves按钮特效 -->
<script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>

<!-- 主题配置脚本 -->
<script>
var BLOG = { ROOT: '/blog/', SHARE: true, REWARD: true };
</script>

<!-- jquery -->
<script src="/blog/js/jquery.min.js?v=3.0"></script>

<!-- 搜索 -->

<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item waves-block waves-effect" onclick="location.href='{path}'">
    <div class="title ellipsis" title="{title}">{title}</div>
</li>
</template>


<!-- main博客脚本 -->
<script src="/blog/js/main.min.js?v=3.0" ></script>

<!-- 动画&配置 -->
<script src="/blog/js/script.min.js?v=3.0" ></script>

<!-- 脚本管理 -->
<script>

if(window.innerWidth > 800){
	/* 3D标题 */
	$(".content-header").on("mousemove", threedee);

	/* 底部追随鼠标 */
	$(".footer").hover(2);

	/* gotop键的涟漪 */
	$("#gotop").hover(1);

	/* 赞赏的粒子雨 */
	$("#reward").hover(3);

	/* 微信公众号的底部渲染 */
	$("#wechat").hover(4);

    /* 标题跳动 */
    $(".archivestitle").bumpyText();

	/* 图片点击放大 */
	const postimg = jQuery(".post-content img:not(.github-emoji)");
	postimg.on("click",function(){

		mask.classList.add("in");
		main.classList.add("Mask");
		menu.classList.add("Mask");
		var myimg = this.cloneNode(true);
		myimg.classList.add("imgShow");

		setTimeout(function(){
			jQuery(myimg).animate({
				opacity:"1"
			},1000);
		},0);

		document.body.appendChild(myimg);

		myimg.onclick=function(){
			document.body.removeChild(myimg);
			mask.classList.remove("in");
			main.classList.remove("Mask");
			menu.classList.remove("Mask");
		};

	});

}

/* 名字跳动 */
$("#name").bumpyText();


/* 网站运行时间 */
// setInterval(function () {
// 	setTime("2021/3/27");
// }, 1000);

/* 文章块的淡出 */
postshow();

/* 座右铭 */
// 
//    getHitokoto();
// 


/* 粘贴提示 */
G($(".post-content"), location.href, "Nefelibata");


/* 控制台 */
if (window.console && window.console.log) {
	setTimeout(function () {
		console.log("\n %c Nefelibata %c  © kpl0111  http://nefelibata.icu \n\n", "color:#FFFFFB;background:#1abc9c;padding:5px 0;border-radius:.5rem 0 0 .5rem;", "color:#FFFFFB;background:#080808;padding:5px 0;border-radius:0 .5rem .5rem 0;");
	}, 0);
}

</script>




<!-- 公式渲染 -->

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';
    }
});
</script>

<script async src="//cdn.bootcss.com/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_CHTML" async></script>



<!-- 不蒜子 -->
<!--  -->

</body>
</html>
